<?php

/**
 * 
 * @author truonghm
 * @date 06/04/2011
 * dynamic setup extjs grid 
 *
 */

class Ofx_Controller_ExtjsGrid  {
	
	/**
	 * 
	 * @var String $_gridKey ma grid dinh nghia trong data base (table grid)
	 */
	private $_gridKey;
	/**
	 * 
	 * @var object $_reader thong tin cau hinh reader cua grid	 * 
	 */
	private $_reader;
	/**
	 * 
	 * @var object $_store thong tin cau hinh store
	 */
	private $_store;
	/**
	 * 
	 * @var object $_column thong tin cau hinh column
	 */
	private $_columnMode;
	/**
	 * 
	 * @var object $_selMode thong tin cau hinh cell
	 */
	private $_selMode;
	/**
	 * 
	 * @var string $_reader_root key save du lieu tra trong string json tra ve
	 * default = data 
	 */
	private $_reader_root="data";
	/**
	 * 
	 * @var string $_readerTotalProperty tong so ban ghi tra ve trong string json
	 * default = count
	 */
	private $_readerTotalProperty="count";
	
	/**
	 * 
	 * @var object $_grid thong tin cau hinh grid
	 */
	private $_grid;
	
	/**
	 * 
	 * @var object $_paging thong tin phan trang
	 */
	private $_paging;
	
	/**
	 * 
	 * @param string $p_gridKey
	 * @param string $p_reader_root
	 * @param string $p_readerTotalProperty
	 * @desc khoi tao dinh nghia grid
	 * Thong tin cau hinh grid duoc dinh nghia trong bang grid va grid_config
	 */
	public function __construct($p_gridKey,$p_reader_root=null,$p_readerTotalProperty=null){
		
		$this->_gridKey = $p_gridKey;
		if($p_reader_root!=null) $this->_reader_root = $p_reader_root;
		if($p_readerTotalProperty!=null) $this->_readerTotalProperty = $p_readerTotalProperty;
		
		//lay thong tin config trong data
		$lbl_grid = new Models_Grid();
		$arr = $lbl_grid->getGridConfig($this->_gridKey);
		
		//khoi tao va add cau hinh reader
		$this->setReader($arr);
		
		//khoi tao va add cau hinh column
		$this->setColumn($arr);	
		
		//khoi tao selMode		
		$this->setSelMode();

		//khoi tao store
		$this->setStore();
		
		//khoi tao paging
		$this->setPaging();
		
		//set grid
		$this->setGrid();
	}
	
	/**
	 * 
	 * @param $arr thong tin cau hinh reader
	 * @desc add cau hinh reader
	 */
	private function setReader($arr){
		
		$this->_reader = new PhpExt_Data_JsonReader();
		/**
		 * config default reader
		 */			
		$this->_reader->setRoot($this->_reader_root)
			   		  ->setTotalProperty($this->_readerTotalProperty);

		if(!$arr) return;
		
		foreach($arr as $key=>$value){
			$this->_reader->addField(new PhpExt_Data_FieldConfigObject(
					 $value["data_index"]
					,$value["red_mapping"]
					,$value["red_type"]
					,$value["red_date_format"]
					,$value["red_sort_type"]
					,$value["red_sort_dir"]
					,$value["red_convert"]									
			  ));
		}
	}
	
	
	
	/**
	 * 
	 * @param $arr thong tin cau hinh reader
	 * @desc get cau hinh reader
	 */
	public function getReader(){
		return $this->_reader;
	}
	
	
	/**
	 * 
	 * @param $arr thong tin cau hinh reader
	 * @desc get cau hinh column
	 */
	public function getColumn(){
		return $this->_columnMode;
	}
	
	
	/**
	 * 
	 * @param $arr thong tin cau hinh column
	 * @desc add cau hinh column
	 */
	private function setColumn($arr){
		$translate=Zend_Registry::get('translate');
		$this->_columnMode = new PhpExt_Grid_ColumnModel();
		$this->_columnMode->addColumn(new PhpExt_Grid_RowNumberer());
		if(!$arr) return;
		
		foreach($arr as $key=>$value){
			$vn_cols_is_grid = $value["cols_is_grid"];			
			
			
			if($vn_cols_is_grid==1){
				$vc_header = $translate->translate($value["cols_header"]);
				$vc_dataIndex = $value["data_index"];
				$vc_colsId = $value["data_index"];
				$vn_width = $value["cols_width"];
				$vc_align = $value["cols_align"];				
				
				$vc_renderer=null;
				if($value["cols_renderer"]) $vc_renderer = PhpExt_Javascript::variable($value["cols_renderer"]);
				
				$bl_sortable = ($value["cols_sortable"]>0);
				$bl_locked = ($value["cols_locked"]>0);
				$bl_hidden = ($value["cols_hidden"]>0);
			
				$this->_columnMode->addColumn(PhpExt_Grid_ColumnConfigObject::createColumn(
						 $vc_header
						,$vc_dataIndex
						,$vc_colsId
						,$vn_width
						,$vc_align
						,$vc_renderer
						,$bl_sortable
						,$bl_locked
						,$bl_hidden				
				));
			}
		}
	}
	
	/**
	 * khoi tao celMode
	 */
	private function setSelMode(){
		$this->_selMode = new PhpExt_Grid_RowSelectionModel();
	}
	
	
	/**
	 * get thong tin celMode
	 */
	public function getSelMode(){
		return $this->_selMode;
	}
	
	
	/**
	 * khoi tao store
	 */
	private function setStore()
	{
		$this->_store = new PhpExt_Data_Store();
		$this->_store->setReader($this->getReader());
	}
	
	/**
	 * get store
	 */
	public function getStore()
	{
		return $this->_store;
	}
	
	/**
	 * khoi tao grid
	 */
	private function setGrid(){
		$this->_grid = new PhpExt_Grid_GridPanel();
		
		$this->_grid->setStore($this->getStore())
				    ->setColumnModel($this->getColumn())
				    ->setSelectionModel($this->getSelMode())
				    ->setLoadMask(true)
				    ->setBottomToolbar($this->getPaging())
				    ->setId($this->_gridKey);
	}
	
	
	/**
	 * get grid
	 */
	public function getGrid(){
		return $this->_grid;
	}
	
	
	
	/**
	 * khoi tao paging grid
	 */
	private function setPaging(){
		$translate=Zend_Registry::get('translate');
		
		$this->_paging = new PhpExt_Toolbar_PagingToolbar();
		$this->_paging->setStore($this->getStore())
		       ->setPageSize(Zend_Registry::get('session')->page_size)
		       ->setDisplayInfo($translate->translate("Displaying")." {0} - {1} ".$translate->translate("Total")." {2}")
		       ->setEmptyMessage($translate->translate("No topics to display"));
	}
	
	/**
	 * get paging grid
	 */
	public function getPaging(){
		return $this->_paging;
	}	

}


?>